package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.ChildrenBehaviorPurpose;
import orm.CollectivityMonthDomain;
import orm.CollectivityWeekArrange;
import orm.CollectivityWeekTimeBucket;
import orm.IndividualWeekDomain;
import util.DBHelper;

public class IWeekDomainDAO {
	
	
	public void insertIWeekDomain(Statement statement, 
			ArrayList<IndividualWeekDomain> iwdArr, String iWeekArrangeID) throws SQLException {
		IndividualWeekDomain iwd = null;
		for (int i = 0; i < iwdArr.size(); i++) {
			iwd = iwdArr.get(i);
			statement.execute("insert into individualweekdomain values('" + iWeekArrangeID + "'," +
					iwd.getDomainID() + ", '" + iwd.getRemark() + "')");
		}
	}
	
	public void updateIWeekDomain(Statement statement,
			ArrayList<IndividualWeekDomain> iwdArr, String iWeekArrangeID) throws SQLException {
		IndividualWeekDomain iwd = null;
		for (int i = 0; i < iwdArr.size(); i++) {
			iwd = iwdArr.get(i);

			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update individualweekdomain set Remark = '" + iwd.getRemark() + "'");
			sqlStr.append("where IWeekArrangeID = '" + iwd.getIWeekArrangeID() + "' and DomainID = " + iwd.getDomainID() + "");
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteIWeekDomain(Statement statement, String iWeekArrangeID) throws SQLException {
		statement.execute("delete from individualweekdomain where IWeekArrangeID='" + iWeekArrangeID + "'");
	}
	
	public ArrayList<IndividualWeekDomain> getiwdByCMAID(String iWeekArrangeID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		ArrayList<IndividualWeekDomain> al = new ArrayList<IndividualWeekDomain>();
		IndividualWeekDomain iwd = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from individualweekdomain where IWeekArrangeID='" + iWeekArrangeID + "'");
		
		while (rs.next()) {
			iwd = new IndividualWeekDomain();
			int i = 1;
			iwd.setIWeekArrangeID(rs.getString(i++));
			iwd.setDomainID(rs.getInt(i++));
			iwd.setRemark(rs.getString(i++));
			al.add(iwd);
		}
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
}
